package com.cqyc.imview.fragment

import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.os.Bundle
import android.view.View
import androidx.localbroadcastmanager.content.LocalBroadcastManager
import com.cqyc.imview.R
import com.cqyc.imview.activity.HomeSettingActivity
import com.cqyc.imview.base.BaseFragment
import com.cqyc.imview.databinding.FragmentConversationBinding
import com.cqyc.imview.model.ConversationModel
import com.cqyc.network.getid.GetMyInformationBox
import com.tencent.qcloud.tuicore.TUIConstants
import com.tencent.qcloud.tuikit.tuiconversation.ui.page.TUIConversationFragment

//isBuyer是否是买家家端
class ConversationFragment:BaseFragment<FragmentConversationBinding, ConversationModel>(), View.OnClickListener {

    private lateinit var fragment: TUIConversationFragment
    private lateinit var unreadCountReceiver: BroadcastReceiver

    override fun setLayout(): Int {
        return R.layout.fragment_conversation
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        val beginTransaction = childFragmentManager.beginTransaction()
        fragment = TUIConversationFragment()
        beginTransaction.replace(R.id.fragment, fragment)
        beginTransaction.commit()

        mBinding.model = mViewModel
        mViewModel.isBuyer.value = GetMyInformationBox.isBuyer()
        mBinding.onClick = this

        if (GetMyInformationBox.isBuyer()) {
            initUnreadCountReceiver()
        }

        //引导
        val sp = requireContext().getSharedPreferences("ConversationFragment", Context.MODE_PRIVATE)
        val isFirst = sp.getBoolean("isFirst", true)
        if (isFirst) {
            mBinding.guideInclude.visibility = View.VISIBLE
            mBinding.guideInclude.setOnClickListener {
                mBinding.guideInclude.visibility = View.GONE
                val edit = sp.edit()
                edit.putBoolean("isFirst", false)
                edit.apply()
            }
        }
    }

    fun updateNoReadMsg(int: Int) {
        mViewModel.noReadMsg.value = int
    }

    override fun onClick(p0: View?) {
        when (p0!!.id) {
            R.id.back -> {
                requireActivity().finish()
            }
            R.id.setting -> {
                val intent = Intent(context, HomeSettingActivity::class.java)
                startActivity(intent)
            }
            R.id.clean_up -> {
                fragment.cleanUp()
            }
        }
    }

    private fun initUnreadCountReceiver() {
        unreadCountReceiver = object : BroadcastReceiver() {
            override fun onReceive(context: Context, intent: Intent) {
                val unreadCount = intent.getLongExtra(TUIConstants.UNREAD_COUNT_EXTRA, 0)
                updateNoReadMsg(unreadCount.toInt())
            }
        }
        val unreadCountFilter = IntentFilter()
        unreadCountFilter.addAction(TUIConstants.CONVERSATION_UNREAD_COUNT_ACTION)
        LocalBroadcastManager.getInstance(requireContext())
            .registerReceiver(unreadCountReceiver, unreadCountFilter)
    }
}